﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Collections;

namespace TuyenDungViecLam.Admin
{
    public partial class ManageRole : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                IEnumerable dsRole = User_BUS.GetAllRole();

                DrlRole.DataSource = dsRole;
                DrlRole.DataTextField = "Name";
                DrlRole.DataValueField = "RoleID";
                DrlRole.DataBind();

                //do du lieu len gridview

                LoadDataToGridView();
            }
        }

        private void LoadDataToGridView()
        {
            GrvManageRole.DataSource = User_BUS.GetUserInRole(int.Parse(DrlRole.SelectedValue.ToString()));
            GrvManageRole.DataBind();
        }

        protected void GrvManageRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label Status = (Label)e.Row.FindControl("Status");
                CheckBox cbchon = (CheckBox)e.Row.FindControl("cbchon");
                if ((int.Parse(Status.Text)) == 1)
                {
                    cbchon.Checked = true;
                }
                Label lbuser = (Label)e.Row.FindControl("lbuser");
                ImageButton Delete = (ImageButton)e.Row.FindControl("Delete");
                string str = "if(!confirm('Bạn có muốn xóa " + lbuser.Text + " này không?')) return false;";
                Delete.OnClientClick = str; //"if(!confirm('Bạn không muốn xóa công việc này không ?')) return false;";
            }
        }

        protected void GrvManageRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GrvManageRole.PageIndex = e.NewPageIndex;
            LoadDataToGridView();
         
        }

        protected void DrlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            //System.Threading.Thread.Sleep(5000);
            LoadDataToGridView();
        }

        protected void GrvManageRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int userID = int.Parse(e.CommandArgument.ToString());

                bool kq = User_BUS.DeleteRoleUser(userID,int.Parse(DrlRole.SelectedValue.ToString()));

                if (kq == true)
                {
                    LoadDataToGridView();
                }
            }
        }

        protected void GrvManageRole_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}
